// pages/cansai/cansai.js
Page({
  tijiao(){
    var that = this;
    var matchid = this.data.matchid;
    var index = this.data.category+'';
    var userid = this.data.userid;
    var username = this.data.username;
    var time = new Date();
    var image = null; 
    wx.chooseMedia({
      count: 1,
      success(res) {
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: matchid+index+userid +new Date().getTime()+ ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log(res.fileID);
                  image = res.fileID;
                  that.setData({
                    currentImage:image
                  });
                  var database = wx.cloud.database();
                  var data = {
                    matchid:matchid,
                      index:index,
                      userid:userid,
                      username:username,
                      time:time,
                      image:image
                  }
                  database.collection("tb_item").add({
                    data
                  }).then((s)=>{
                    if(s)wx.showToast({
                      title: '提交图片成功',
                    })
                    that.setData({
                      isDisabled:true
                    })
                    var time = data.time;
                    var d = time.toLocaleDateString().substring(0,10);
                    var t = time.toTimeString().substring(0,8);
                    time = d+""+t;
                    data.time = time;
                    that.data.xvanshou.push(data);
                    that.setData({
                      xvanshou:that.data.xvanshou
                    })
                  })
              }
          })
      }
  })
  },

  dianji(){
    this.setData({
      dialogShow_match:true,
      currentImage:this.data.picurls[this.data.category]
    })

  },
  click:function(e){

    this.setData({
      category:e.currentTarget.id+""
    });
    var that = this;
    var database = wx.cloud.database();
      database.collection("tb_item").where({
        matchid:that.data.matchid,
        index:that.data.category+""
      }).get().then((res)=>{
         that.setData({
           xuanshou:res.data
         });
         that.setData({
           isDisabled:false
         })
         for(let i=0;i<that.data.xuanshou.length;i++){
          var time = that.data.xuanshou[i].time;
          var d = time.toLocaleDateString().substring(0,10);
          var t = time.toTimeString().substring(0,8);
          time = d+" "+t;
          that.data.xuanshou[i].time = time;
          if(that.data.xuanshou[i].userd == that.data.userd){
            that.setData({
              isDisabled:true
            })
          }
        }
        that.setData({
          xuanshou:that.data.xuanshou
        });

        })

  },


    /**
     * 页面的初始数据
     */
    data: {
      matchid:null,
      match:null,
      picnames:[],
      category:0,
      picurls:[],
      index:0,
      dialogShow_match:false,
      currentImage:null,
      userid:null,
      username:null,
      xuanshou:[],
      isDisabled:false



    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this;
      wx.getStorage({
        key:"matchid",
        success(res){
          console.log('storage数据:'+res.data);
          that.data.matchid = res.data;
          that.setData({
                    matchid:that.data.matchid
                });
            console.log('match_id:'+that.data.matchid);
            var database = wx.cloud.database();
            database.collection("tb_match").where({
                _id:that.data.matchid
            }).get().then((res)=>{
                if(res.data.length>0){
                  that.setData({
                    match:res.data[0]
                  });
                  var picnames = [];
                  var match = that.data.match;
                  var picurls = [];
                  for(let i=0;i<match.imageList.length;i++){
                    var data = match.imageList[i];
                    picurls.push(''+data);
                     var loc = data.lastIndexOf("/");
                     data = data.slice(loc+1);
                    loc = data.lastIndexOf("_");
                    data =  data.slice(0,loc);
                    picnames.push(data);
                  }
                  that.setData({
                    picnames:picnames,
                    picurls:picurls
                  });
                }
              });
        }
        });

        wx.getStorage({
          key:"userid",
          success(res){
             that.setData({
               userid:res.data
             });
             //到数据库里面找到当前用户参加的比赛
             var database = wx.cloud.database();
             database.collection("tb_user").where({
               _id:that.data.userid
             }).get().then((r)=>{
               that.setData({
                 username:r.data[0].name
               });
             })
          }
        })
        

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
      var that = this;
      var database = wx.cloud.database();
        database.collection('tb_item').where({
          matchid:that.data.matchid,
          index:0+"" 
        }).get().then((g)=>{
          that.setData({
            xuanshou:g.data});
            for(let i=0;i<that.data.xuanshou.length;i++){
              var time = that.data.xuanshou[i].time;
              var d = time.toLocaleDateString().substring(0,10);
              var t = time.toTimeString().substring(0,8);
              time = d+" "+t;
              that.data.xuanshou[i].time = time;
              /*判断当前选手是否已经在达到选手当中*/
              if(that.data.xuanshou[i].userd == that.data.userd){
                that.setData({
                  isDisabled:true
                })
              }
            }
            that.setData({
              xuanshou:that.data.xuanshou
            });
   
          })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})